package com.msmall.community.weather.action.html;

import java.util.List;

import com.msmall.base.action.BaseAction;
import com.msmall.community.common.model.City;
import com.msmall.community.common.service.CityManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;
import com.msmall.community.weather.model.ForeCast;
import com.msmall.community.weather.model.Weather;
import com.msmall.community.weather.service.WeatherManager;

public class WeatherAction extends BaseAction {

	private static final long serialVersionUID = -3837513493394971019L;

private String city; //城市行政区划码
	
	private String cityName; //城市中文名称 默认城市名称
	
	private String content;//搜索内容

	private List<City> cityList;
	
	private Weather weather;
	
	private ForeCast foreCast;
	
	private WeatherManager weatherManager;
	
	private UserManager userManager;
	
	private CityManager cityManager;
	
	/**
	 * 进入首页
	 */
	public String execute() throws Exception {
		try{
			initDefaultCity();
			weather = weatherManager.getWeather(city);
			if(weather.getForeCasts() != null && weather.getForeCasts().size() > 0){
				foreCast = weather.getForeCasts().get(0);
			}
		}catch(Exception e){
			return ERROR;
		}
		return SUCCESS;
	}
	
	private void initDefaultCity(){
		try {
		//	UserInfo usrinfo = userManager.getByUsername(getUsername());
		//	City city = cityManager.getCityByCode(usrinfo.getWeatherCode());
		//	setCityName(city.getCityName());
		} catch (Exception e) {
			setCityName(WeatherManager.DEFAULT_WEATHER_NAME);
		}
	}
	/**
	 * 查看72小时天气
	 * @return
	public String more() throws Exception {
		try{
			initDefaultCity();
			weather = weatherManager.getWeather(city);
			if(weather.getForeCasts() != null && weather.getForeCasts().size() > 0){
				foreCast = weather.getForeCasts().get(0);
			}
		}catch(Exception e){
			return ERROR;
		}
		return SUCCESS;
	}
	*/
	/**
	 * 切换城市首页,
	 * @return
	 */
	public String change() throws Exception {
		return SUCCESS;
	}
	/**
	 * 根据省名称查询所有城市
	 */
	public String cities() throws Exception {
		if(CityManager.DIRECT_CITY_CODE.indexOf(city) != -1){
			initDefaultCity();
			try{ // 直辖市， 查看天气
				weather = weatherManager.getWeather(city);
				if(weather.getForeCasts() != null && weather.getForeCasts().size() > 0){
					foreCast = weather.getForeCasts().get(0);
				}
				return "city";
			}catch(Exception ex){
				return ERROR;
			}
		}
		cityList = cityManager.getWeatherChildCities(city);
		return SUCCESS;
	}
	/**
	 * /根据**查询天气
	 */
	
	public String search() throws Exception {
		try{
			City cityx = cityManager.getCityByEnglishName(content);
			weather = weatherManager.getWeather(cityx.getCityCode());
			if(weather.getForeCasts() != null && weather.getForeCasts().size() > 0){
				foreCast = weather.getForeCasts().get(0);
			}
			initDefaultCity();
		}catch(Exception e){
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 设置默认城市
	 */
	public String set() throws Exception {
		try{
			City cityx = cityManager.getCityByCode(city);
		//	UserInfo usr = userInfoManager.getByUserid(getUserid());
		//	usr.setWeatherCode(city);
		//	userInfoManager.saveObject(usr);
			setCityName(cityx.getFullName());
		}catch(Exception e){
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public Weather getWeather() {
		return weather;
	}

	public void setWeather(Weather weather) {
		this.weather = weather;
	}

	public ForeCast getForeCast() {
		return foreCast;
	}
	public void setForeCast(ForeCast foreCast) {
		this.foreCast = foreCast;
	}
	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}

	public void setCityManager(CityManager cityManager) {
		this.cityManager = cityManager;
	}

	public void setWeatherManager(WeatherManager weatherManager) {
		this.weatherManager = weatherManager;
	}
}
